﻿BitwiseOperationsTests = function () {
    this.Name = "Bitwise Operations Tests";
};

BitwiseOperationsTests.prototype = new TestFixture();


// - and -
// 0111010011110 = 3742
// 0110110011010 = 3482
// -------------
// 0110010011010 =  3226
BitwiseOperationsTests.prototype.TestAnd = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 3742");
    vm.Execute("ldc.i4 3482");
    vm.Execute("and");
    var result = vm.State.Stack.Top();
    this.Assert(3226, result.Value);
};

// - or -
// 0111010011110 = 3742
// 0110110011010 = 3482
// -------------
// 0111110011110 =  3998
BitwiseOperationsTests.prototype.TestOr = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 3742");
    vm.Execute("ldc.i4 3482");
    vm.Execute("or");
    var result = vm.State.Stack.Top();
    this.Assert(3998, result.Value);
};

// - xor -
// 0111010011110 = 3742
// 0110110011010 = 3482
// -------------
// 0001100000100 =  772
BitwiseOperationsTests.prototype.TestXor = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 3742");
    vm.Execute("ldc.i4 3482");
    vm.Execute("xor");
    var result = vm.State.Stack.Top();
    this.Assert(772, result.Value);
};

// - not -
// 0111010011110 = 3742
// -------------
// 1000101100001 =  -3743
BitwiseOperationsTests.prototype.TestNot = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 3742");
    vm.Execute("not");
    var result = vm.State.Stack.Top();
    this.Assert(-3743, result.Value);
};

BitwiseOperationsTests.prototype.TestShiftLeft = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 129");
    vm.Execute("ldc.i4 3");
    vm.Execute("shl");
    var result = vm.State.Stack.Top();
    this.Assert(1032, result.Value);
};

BitwiseOperationsTests.prototype.TestShiftRight = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 16");
    vm.Execute("ldc.i4 3");
    vm.Execute("shr");
    var result = vm.State.Stack.Top();
    this.Assert(2, result.Value);
};

BitwiseOperationsTests.prototype.TestShiftRightWithSign = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 -16");
    vm.Execute("ldc.i4 3");
    vm.Execute("shr");
    var result = vm.State.Stack.Top();
    this.Assert(-2, result.Value);
};

BitwiseOperationsTests.prototype.TestShiftRightUnsigned = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 -16");
    vm.Execute("ldc.i4 3");
    vm.Execute("shr.un");
    var result = vm.State.Stack.Top();
    this.Assert(536870910, result.Value);
};

BitwiseOperationsTests.prototype.TestNeg = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 -16");
    vm.Execute("neg");
    var result = vm.State.Stack.Top();
    this.Assert(16, result.Value);
};
